package com.blb.day20231116redis.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.day20231116redis.mapper.ScoreMapper;
import com.blb.day20231116redis.pojo.Score;
import com.blb.day20231116redis.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {

    public static final String PREFIX = "Score:";

    @Autowired
    private ScoreMapper scoreMapper;

//    @Autowired
//    private RedisTemplate<String,Object> redisTemplate;

    //声明式缓存，加到service查询方法上，读取数据库后保存到redis中，cacheNames设置键的前缀，key设置键值
    @Cacheable(cacheNames = "Score",key = "T(String).valueOf(#id)")
    @Override
    public Score getScoreById(Long id) {
        return scoreMapper.selectById(id);
//        //获得string类型操作对象
//        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
//        //先查询redis，redis没有再查mysql
//        Score value = (Score) ops.get(PREFIX + id);
//        if(value == null){
//            Score score = scoreMapper.selectById(id);
//            //查询数据库不为空，就保存到redis中
//            if(score != null){
//                ops.set(PREFIX + id,score);
//                return score;
//            }
//            return null;
//        }
//        //如果redis存在就直接返回
//        return value;
    }

    @Cacheable(cacheNames = "Scores")
    @Override
    public List<Score> getAll() {
        return scoreMapper.selectAll();
    }

    @Cacheable(cacheNames = "ScorePage",key = "T(String).valueOf(#current)")
    @Override
    public IPage<Score> getScorePage(Long current, Long size) {
        return scoreMapper.selectScorePage(new Page<>(current,size));
    }

    //加到更新或添加方法上，根据key判断，是添加或更新缓存
//    @CachePut(cacheNames = "Score",key = "T(String).valueOf(#score.id)")
    //组合式注解，更新Score缓存时，同时删除Scores和ScorePage两个缓存
    @Caching(
            put = @CachePut(cacheNames = "Score",key = "T(String).valueOf(#score.id)"),
            evict = {
                    @CacheEvict(cacheNames = "Scores",allEntries = true),
                    @CacheEvict(cacheNames = "ScorePage",allEntries = true),
            }
    )
    @Override
    public Score updateScore(Score score) {
        scoreMapper.updateById(score);
        return score;
    }

    @CachePut(cacheNames = "Score",key = "T(String).valueOf(#score.id)")
    @Override
    public Score saveScore(Score score) {
        scoreMapper.insert(score);
        return score;
    }

    //加删除方法，同时删除缓存
    @CacheEvict(cacheNames = "Score",key = "T(String).valueOf(#id)")
    @Override
    public void deleteScore(Long id) {
        scoreMapper.deleteById(id);
    }
}
